package cn.ym.mall.service.impl;

import cn.ym.mall.dao.RoleRightsDao;
import cn.ym.mall.dao.impl.RoleRightsDaoImpl;
import cn.ym.mall.entity.RoleRights;
import cn.ym.mall.service.RoleRightsService;
import cn.ym.mall.utils.DruidUtils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleRightsServiceImpl implements RoleRightsService {
    RoleRightsDao roleRightsDao = new RoleRightsDaoImpl();

    /**
     * 获得所有角色权限编号
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<RoleRights> getAll() throws SQLException {
        Connection conn = null;
        List<RoleRights> roleRightsList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            roleRightsList = roleRightsDao.getAll();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return roleRightsList;
    }

    /**
     * 添加新角色权限编号
     *
     * @param roleRights
     * @return
     * @throws SQLException
     */
    @Override
    public boolean add(RoleRights roleRights) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (roleRightsDao.add(roleRights) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据角色Id删除角色权限编号
     *
     * @param rid
     * @return
     * @throws SQLException
     */
    @Override
    public boolean delete(Integer rid) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (roleRightsDao.delete(rid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 更新角色权限编号
     *
     * @param roleRights
     * @return
     * @throws SQLException
     */
    @Override
    public boolean update(RoleRights roleRights) throws SQLException {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if (roleRightsDao.update(roleRights) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 根据角色id获得角色权限编号
     *
     * @param rid
     * @return
     * @throws SQLException
     */
    @Override
    public RoleRights getRoleRightsByRid(Integer rid) throws SQLException {
        Connection conn = null;
        RoleRights roleRights = null;
        try {
            conn = DruidUtils.getConnection();
            roleRights = roleRightsDao.getRoleRightsByRid(rid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return roleRights;
    }
}
